<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button id="btn">
    测试按钮
  </button>
  <script>
    const formatTime = (date, fmt) => {
      if (!date) return 'is not time';
      // 判断date是不是时间戳
      if (typeof date == ' ') {
        date = new Date(date);
      }
      //获取年份
      if (/(y+)/.test(fmt)) {
        let dateY = date.getFullYear() + '';
        //RegExp.$1 在判断中出现过，且是括号括起来的，所以 RegExp.$1 就是 "yyyy"
        fmt = fmt.replace(RegExp.$1, dateY.substr(4 - RegExp.$1.length));
      }

      //获取其他
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds(),
      };
      for (const k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + '';
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length == 1 ? str : ('00' + str).substr(str.length),
            //
          );
        }
      }
      return fmt;
    };
    btn.addEventListener('click', async () => {
      console.log(formatTime(Date.now(), 'yyyy-MM-dd hh:mm:ss'))
    })

  </script>
</body>

</html>